package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.product.client.common.result.Result;
import com.atguigu.gmall.product.service.ManageService;
import com.atguigu.gmall.product.client.model.product.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@CrossOrigin
@Api(tags = "商品基础属性接口")
@RestController
@RequestMapping("admin/product")
public class ManageController {

    @Autowired
    private ManageService manageService;

    //一级分类
    @GetMapping("getCategory1")
    public Result getCategory1(){
        List<BaseCategory1> list=manageService.getCategory1();
        return Result.ok(list);
    }
    //二级分类
    @GetMapping("getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id){
        List<BaseCategory2> list=manageService.getCategory2(category1Id);
        return Result.ok(list);
    }
    //三级分类
    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id){
        List<BaseCategory3> list=manageService.getCategory3(category2Id);
        return Result.ok(list);
    }
    //根据分类查询平台属性及属性值
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
            @PathVariable("category1Id") Long category1Id,
            @PathVariable("category2Id") Long category2Id,
            @PathVariable("category3Id") Long category3Id){
        return Result.ok(manageService.getAttrInfoList(category1Id,category2Id,category3Id));
    }
    //保存平台属性
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    //修改平台属性
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId){
        BaseAttrInfo baseAttrInfo =manageService.getAttrInfo(attrId);
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        return Result.ok(attrValueList);
    }

    //品牌列表查询 分页
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result baseTrademark(
            @PathVariable Integer page,
            @PathVariable Integer limit
    ){
        IPage iPage = manageService.baseTrademark(page,limit);
        return Result.ok(iPage);
    }
    //添加品牌
    @PostMapping("/baseTrademark/save")
    public Result saveTrademark(@RequestBody BaseTrademark baseTrademark){
         manageService.saveTrademark(baseTrademark);
        return Result.ok();
    }

    //SPU列表查询  获取商品集合分页查询
    @GetMapping("/{page}/{limit}")
    public Result findSpuList(
            @PathVariable Integer page,
            @PathVariable Integer limit,
            Long category3Id
    ){
        IPage iPage=manageService.findSpuList(page,limit,category3Id);
        return  Result.ok(iPage);
    }

    //获取品牌集合
    @GetMapping("baseTrademark/getTrademarkList")
    public  Result getTrademarkList(){
        List<BaseTrademark> baseTrademarkList =  manageService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    //获取销售属性
    @GetMapping("/baseSaleAttrList")
    public Result<List<BaseSaleAttr>> baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList= manageService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //添加SPU
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }
    
    //根据spuid获取图片列表  显示到添加sku页面中
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId){

       List<SpuImage> spuImageList= manageService.spuImageList(spuId);
        return  Result.ok(spuImageList);
    }

    //根据spuId获取销售属性 在添加sku时显示
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable("spuId") Long spuId){
       List<SpuSaleAttr> spuSaleAttrList= manageService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    //添加sku
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        manageService.saveSkuInfo(skuInfo);
        return  Result.ok();
    }

    //sku列表分页
    @GetMapping("/list/{page}/{limit}")
    public Result skuList(
            @PathVariable Integer page,
            @PathVariable Integer limit){
        IPage iPage=manageService.skuList(page,limit);
        return  Result.ok(iPage);
    }

    //上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        manageService.onSale(skuId);
        return Result.ok();
    }

    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        manageService.cancelSale(skuId);
        return Result.ok();
    }
}
